import { URL, Toast, ajax } from '../../utils/index'

// pages/scenePractice/scenePractice.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    dataList: [],
    contentShow: true,
    entity: null,
    TabContentLoading: false,
    recordButType: false,
    radarData: {
      list: [
        { name: '清晰度分析', max: 100 },
        { name: '音量分析', max: 100 },
        { name: '流利度分析', max: 100 },
        { name: '准确率分析', max: 100 },
        { name: '语速分析', max: 100 }
      ],
      data: []
    }
  },
  historyScore () {
    let {dataList,active } = this.data
    wx.navigateTo({
      url: `/pages/historyscore/historyscore?id=${dataList[active].questionId}`,
    })
  },
  addclick() {
    let { radarData,dataList,active } = this.data
    radarData.data = [60, 60, 60, 60, 60]
    dataList[active].score = 90
    this.setData({
      radarData: radarData,
      dataList: dataList
    })
  },
  // 上一题按钮事件
  lastChange() {
    let { active, dataList, radarData } = this.data, index = active - 1
    if (index < 0) return this.setData({
      recordButType: false
    }), Toast('请选择下一题作答');
    dataList[index].voiceUrl = '',radarData.data = []
    this.setData({
      active: index,
      recordButType: true,
      dataList: dataList,
      radarData:radarData
    })
  },
  // 下一题按钮事件
  nextChange() {
    let { active, dataList, radarData} = this.data, index = active + 1
    if (active + 1 > dataList.length - 1) return this.setData({
      recordButType: true
    }), Toast('请选择上一题作答');
    radarData.data = [], dataList[index].voiceUrl = ''
    this.setData({
      active: active + 1,
      recordButType: false,
      dataList: dataList,
      radarData:radarData
    })
  },
  // 推荐话术动画 需要在初始化时调用一次,否则不显示
  showContent() {
    this.setData({
      contentShow: !this.data.contentShow
    })
  },
  tabChange(e) {
    console.log(e, '---------------------')
    if (e.type === "change") {
      let { index } = e.detail
      this.setData({
        active: index
      })
    }
  },
  // 练习详情
  getPracticeDetails(id, loading) {
    loading ? '' : ajax.loading();
    return new Promise((resolve, reject) => {
      ajax.post('/applet/practice/queryPracticeHistoryDetail', {
        id: id || 1
      }).then(res => {
        if (res.data.code === 1) {
          let e = res.data.data.practiceHistoryDetail
          console.log(e, '详情')
          this.setData({
            entity: e,
            TabContentLoading: true,
            radar: [["清晰度分析", e.qxdPoint], ["语速分析", e.ysPoint], ["准确率分析", e.zqlPoint], ["流利度分析", e.lldPoint], ["音量分析", e.ylPoint]]
          }), ajax.toast.clear(), resolve(res)
        } else ajax.toast.fail(res.data.msg), reject(res)
      }).catch(e => (ajax.toast.fail('接口错误\n练习详情'), reject(e), console.log(e)))
    })
  },
  submitExercises(e) {
    //ajax.loading()
    return new Promise((resolve, reject) => {
      ajax.post('/applet/model/submitPractice', e).then(res => {
        res.data.code === 1 ? resolve(res) : reject(res)
        /*if (res.data.code === 1) {
          Toast.success('已提交练习')
          resolve(res)
           let {entity} = this.data;
           for (let i in e) entity[i] = e[i];
           this.radarData(entity),this.setData(entity),Toast.success('已提交练习'),resolve(res)
        } else ajax.toast.fail(res.data.msg), reject(res)*/
      }).catch(e => (ajax.toast.fail('接口错误\n提交练习'), reject(e), console.log(e)))
    })
  },
  recordChange(e) {
    let { score, audioUrl } = e.detail, { active, dataList, radarData } = this.data, { ysPoint, ylPoint, zqlPoint, lldPoint, qxdPoint, synthesizeScore } = score, entity = dataList[active]
    wx.downloadFile({
      url: audioUrl,
      success: (res) => {
        if (res.statusCode === 200) {
          dataList[active].voiceUrl = res.tempFilePath
          this.setData({
            dataList: dataList
          })
        }
      }
    })
    entity.practiceNum += 1, entity.score = synthesizeScore, dataList[active] = entity
    radarData.data = [qxdPoint, ylPoint, lldPoint, zqlPoint, ysPoint]
    this.setData({
      dataList: dataList,
      radarData: radarData
    })
    console.log('datalist---------active', dataList, active)
    this.submitExercises({
      ysPoint, ylPoint, zqlPoint, lldPoint, qxdPoint, voiceUrl: audioUrl, questionId: dataList[active].questionId
    })
    console.log('录音回调函数>>>>>>>>>>>>>>>>>>>>>' + JSON.stringify(e), e)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, 'scenePractice------------options')
    if (!options) return Toast({
      type: 'fail',
      message: '请选择练习题',
      onClose: () => wx.navigateBack()
    })
    let [dataList, index] = [JSON.parse(options.dataList), options.index]
    dataList[index].voiceUrl = '';
    this.setData({
      dataList: dataList,
      active: index - 0
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})